<?php
namespace app\common\model;
class Mobile extends Base{
	// 查询手机号码是否微信号结果处理
    function queryWeixinEnd($data,$member_id){
        $map=[
            'member_id'=>$member_id,
        ];
        $time=time();
        foreach($data as $v) {
            $map['tel']=$v['tel'];
            if($v['status']){ // 是微信号
                $eData=[
                    'status'=>1,
                    'e_time'=>$time,
                    'nickname'=>isset($v['nickname'])?$v['nickname']:'',
                    'weixin'=>isset($v['weixin'])?$v['weixin']:'',
                ];
                $has=$this->read($map,'id');
                if(empty($has)){
                    $eData['tel']=$v['tel'];
                    $eData['s_time']=$time;
                    $eData['member_id']=$member_id;
                    $this->add($eData);
                }else{
                    $this->edit($eData,$map);
                }
            }else{ //非微信号
                $this->edit(['status'=>2,'e_time'=>$time],$map);
            }
            
        }
    }
    /**
     * 查询手机号是否微信号任务
     * @param  array $data 由controller('member/task')->newTask()提供
     * @param  integer $uid  用户ID
     * @return array
     */
    function createQueryWx($data,$uid){
        $software=$data['software'];
        $number=$data['options']['number'];
        $number=array_filter($number);
        if(empty($software)||empty($number)){
            return error();
        }
        $c=ceil(count($number)/count($software));
        $softModel=model('software');
        $mobileModel=model('mobile');
        foreach ($software as $k => $v) {
            $soft=$softModel->read(['member_id'=>$uid,'id'=>$v],'id,device_id');
            if(empty($soft)){
                array_splice($software, $k,1);
                $data['software']=$software;
                return $this->generateData($data,$uid);
                break;
            }
            $data['dev']=[$soft['device_id']];
            $data['software']=[$soft['id']];
            $queryNumber=array_splice($number, 0,$c);
            $data['options']['number']=$queryNumber;
            // 创建任务 
            $res=controller('task')->createTask($data,$uid,[]);
            if($res['success']){
                $mobileModel->edit(['status'=>3],['tel'=>['tel','in',$queryNumber]]);
            }
        }
        return success();
    }
}
